package com.ruoyi.combat.util;

import cn.hutool.core.thread.AsyncUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RuntimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
@Slf4j
public class Topic5Util {
    @Value("${keti5.funcation0Path}")
    private String funcation0Path;


    public void GAlign_OK(String type, String source_dataset, String target_dataset, String groundtruth, String taskId) {
//        String[] split = funcation0Path.split(":");
//        String s = split[0];
//        String cmd0 = s + ":";
//        String cmd1 = "cd " + funcation0Path;
        String cmd1 = funcation0Path;
        String cmd2 = "python -u network_alignment.py --source_dataset " + source_dataset + " --target_dataset " + target_dataset + " --groundtruth " + groundtruth + " --taskId " + taskId + " " + type;

        List<String> list = new ArrayList<>();
//        list.add(cmd0);
        list.add(cmd1);
        list.add(cmd2);
        ThreadUtil.execAsync(new CmdTask(list));
    }






    public void executeCMD(List<String> commands) {
        String command = handlerCMD(commands);
        log.info(command);
        try {

            ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", commands.get(1));
            processBuilder.directory(new File(commands.get(0)));
            processBuilder.redirectErrorStream(true);
            Process process = processBuilder.start();
            String result = new BufferedReader(new InputStreamReader(process.getInputStream())).lines().collect(Collectors.joining(System.lineSeparator()));
            log.info(result);
            process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 处理命令行
     *
     * @param commands 使用 &&符号拼接多个命令行
     * @return 处理后的命令行
     */
    private static String handlerCMD(List<String> commands) {
        StringBuilder command = new StringBuilder();
        for (int i = 0; i < commands.size(); i++) {
            command.append(commands.get(i));
            if (i != commands.size() - 1) {
                command.append(" && ");
            }
        }
        return command.toString();
    }


    /**
     * 相似度计算
     *
     * @param str1
     * @param str2
     * @return
     */
    public double calculate(String str1, String str2) {
        Set<Character> set1 = new HashSet<>();
        Set<Character> set2 = new HashSet<>();

        for (char c : str1.toCharArray()) {
            set1.add(c);
        }

        for (char c : str2.toCharArray()) {
            set2.add(c);
        }

        Set<Character> intersection = new HashSet<>(set1);
        intersection.retainAll(set2);

        Set<Character> union = new HashSet<>(set1);
        union.addAll(set2);

        return (double) intersection.size() / union.size();
    }


}

